//
//  ThirtysevenViewController.m
//  Depressed.domo
//
//  Created by rimi on 16/5/27.
//  Copyright © 2016年 薛涛. All rights reserved.
//

#import "ThirtysevenViewController.h"
#import <CoreData/CoreData.h>
#import "Person.h"
#import "ContextModel.h"
@interface ThirtysevenViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong)UITextField *ageText;
@property (nonatomic ,strong)UITextField *nameText;
@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)ContextModel *contextModel;
@end

@implementation ThirtysevenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"CoreData";
    [self initDataSouce];
    [self initUI];
    

}
-(void)initUI {
    _ageText = [[UITextField alloc]initWithFrame:CRM(50, 600, 150, 40)];
    _ageText.placeholder = @"请输入年龄";
    _ageText.borderStyle = UITextBorderStyleRoundedRect;
    _ageText.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:_ageText];
    
    _nameText = [[UITextField alloc]initWithFrame:CRM(50, 650, 150, 40)];
    _nameText.placeholder = @"请输入姓名";
    _nameText.borderStyle = UITextBorderStyleRoundedRect;
    _nameText.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:_nameText];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CRM(300, 60, 80, 80);
    btn.backgroundColor = [UIColor brownColor];
    [btn setTitle:@"ADD" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btn11:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}

- (void)initDataSouce {
    _contextModel = [ContextModel shareContextModel];
    [_contextModel initWithContext];
    [_contextModel getWithDataSourceArrM];

}
#pragma mark -<UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _contextModel.dataSourceArrM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   static NSString *identifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [UIColor blueColor].CGColor;
    cell.textLabel.text = _contextModel.dataSourceArrM[indexPath.row][@"name"];
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    Person *person = _contextModel.fetchArrM[indexPath.row];
    [_contextModel deleteContextObject:person];
    [self.tableView reloadData];
}
#pragma mark uibutton
- (void)btn11:(UIButton *)sender {
    [_contextModel insertContextObject:_nameText.text ageField:_ageText.text];
    [self.tableView reloadData];
}


#pragma mark get
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CRM(10, 200, 300, 200) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 70;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.layer.borderWidth = 1;
        _tableView.layer.borderColor = [UIColor blackColor].CGColor;
        /**分割线样式*/
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end
